﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Yxinmt.Client.Code
{
    public class LocalUniqueId
    {
        public static string GetValue()
        {
            try
            {
                return Common.ComputeStringMd5(Common.RemoveEnterOrWhiteSpace(GetUniqueId() + GetDiskDeviceCode() + GetBIOSSerialNumber()));
            }
            catch
            {
                return Common.ComputeStringMd5(Common.RemoveEnterOrWhiteSpace(GetUniqueId() + GetDiskDeviceCode()));
            }
        }

        public static string GetDiskDeviceCode()
        {

            SelectQuery query = new SelectQuery("SELECT * FROM Win32_DiskDrive");
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (var item in searcher.Get())
                {
                    using (item)
                    {
                        if (item["SerialNumber"] != null) return item["SerialNumber"].ToString();
                    }
                }
            }

            return string.Empty;
        }

        public static string GetMAC()
        {

            SelectQuery query = new SelectQuery("SELECT * FROM Win32_NetworkAdapter");
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (var item in searcher.Get())
                {
                    using (item)
                    {
                        if (item["GUID"] != null) return item["GUID"].ToString();
                    }
                }
            }

            return string.Empty;
        }

        public static string GetUniqueId()
        {
            SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (var item in searcher.Get())
                {
                    using (item) return item["UUID"].ToString();
                }
            }
            return string.Empty;
        }

        public static string GetBIOSSerialNumber()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
                string sBIOSSerialNumber = "";
                foreach (ManagementObject mo in searcher.Get())
                {
                    sBIOSSerialNumber = mo["SerialNumber"].ToString().Trim();
                }
                return sBIOSSerialNumber;
            }
            catch
            {
                return "";
            }
        }
    }
}
